package com.imooc.miaosha.dao;

import com.imooc.miaosha.pojo.domain.Goods;
import com.imooc.miaosha.pojo.vo.GoodsVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper//注意这是mytatis的注解，而不是spring的
public interface GoodsDao {
    @Select("select * from goods where id=#{id}")
    Goods getById(@Param("id")long id);

    @Select("select a.*,b.miaosha_price,b.stock_count,b.start_date,b.end_date\n" +
            "from goods a\n" +
            "join miaosha_goods b on a.id = b.goods_id")
    List<GoodsVo> listGoodsVo();

    @Select("select goods.*,mg.miaosha_price,mg.stock_count,mg.start_date,mg.end_date " +
            "from goods\n" +
            "join miaosha_goods mg on goods.id = mg.goods_id\n" +
            "where goods.id=#{id}")
    GoodsVo findGoodsVoById(@Param("id") long id);

    @Update("update miaosha_goods set stock_count=IF(stock_count<1,0,stock_count-1) where goods_id=#{id}")
    void reduceStock(@Param("id") long id);

}
